package com.service.impl;

import java.util.List;

import com.controller.CacheController;
import com.controller.FurniturePageResponse;
import com.dao.HyFurnitureMapper;
import com.entity.HyFurniture;
import com.entity.HyFurnitureImage;
import com.entity.HyFurnitureWithImage;
import com.service.HyFurnitureService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Lazy;
import org.springframework.stereotype.Service;


/**
 * 家具列表Service业务层处理
 * 
 * @author 白杨
 * @date 2025-02-14
 */
@Service
public class HyFurnitureServiceImpl implements HyFurnitureService
{
    @Autowired
    private HyFurnitureMapper hyFurnitureMapper;



    // 获取所有家具信息
    public List<HyFurniture> getAllFurniture() {
        return hyFurnitureMapper.selectAll(); // 从数据库获取所有家具信息
    }

    @Override
    public FurniturePageResponse getFurnitureWithImages(int pageNum, int pageSize) {
        int offset = (pageNum - 1) * pageSize;
        List<HyFurnitureWithImage> content = hyFurnitureMapper.getFurnitureWithImages(offset, pageSize);
        long totalElements = hyFurnitureMapper.getTotalFurnitureCount();
        long totalPages = (totalElements + pageSize - 1) / pageSize;
        return new FurniturePageResponse(content, totalElements, totalPages, pageSize, pageNum);
    }

    @Override
    public HyFurniture selectHyFurnitureById(Long furnitureId) {
       return hyFurnitureMapper.selectHyFurnitureById(furnitureId);
    }


    // 更新库存
    public boolean updateStock(Long furnitureId, int quantity) {
        // 执行库存更新，返回更新是否成功
        return hyFurnitureMapper.updatefurnnum(furnitureId, quantity) > 0;
    }

    //根据家具id获取家具信息
    public HyFurniture getFurnitureById(Long id) {
        // 从缓存中获取家具信息
        return  hyFurnitureMapper.selectHyFurnitureById(id);


    }

    //更新库存数量
    public int updatefurnnum(Long furnitureId, int quantity) {
        return hyFurnitureMapper.updatefurnnum(furnitureId, quantity);
    }

    @Override
    public int addFurniture(HyFurniture hyFurniture) {
        return hyFurnitureMapper.insertFurniture(hyFurniture);

    }

    @Override
    public int deleteFurniture(Long id) {
        return hyFurnitureMapper.deleteFurniture(id);
    }

    @Override
    public int updateFurniture(HyFurniture hyFurniture) {
        return hyFurnitureMapper.updateFurniture(hyFurniture);
    }

    @Override
    public int addFurnitureImage(HyFurnitureImage hyFurnitureImage) {
        return hyFurnitureMapper.insertFurnitureImage(hyFurnitureImage);

    }

    @Override
    public int deleteFurnitureImage(Long id) {
        return hyFurnitureMapper.deleteFurnitureImage(id);

    }


    @Override
    public List<HyFurnitureImage> getFurnitureImages(Long furnitureId) {
        return hyFurnitureMapper.getFurnitureImagesByFurnitureId(furnitureId);
    }

    @Override
    public List<HyFurniture> getFurnitureByCategory(Long categoryId) {
        return hyFurnitureMapper.getFurnitureByCategoryId(categoryId);

    }

    /**
     * 根据区县id获取该区下所有的家具
     */
    public List<HyFurniture> findFurnitureByRegionId(Long categoryId)
    {
        return hyFurnitureMapper.findFurnitureByCategoryId(categoryId);
    }



}
